package com.zff.tool;

import com.alibaba.fastjson.JSONObject;

public class WeChatUtil {
/**
 * TODO
 * @version 1.0
 * @author Zff
 * @date 2020/7/20 10:55
 */
//1112223333
    public static final String APPID = "wx21ff014f5ae9fbbb";
    public static final String APPSECRET = "5e778227dc79242aebe8fcd1dae2758e";
    public static final String GET_WEB_ACCESSTOKEN_URL = " https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    //获取用户信息的接口
    public static final String GET_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
    //获取用户的UnionID
    public static final  String GET_USER_UNIONID = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
    /**
     * 通过code换取网页授权access_token
     */
    public static JSONObject getWebAccesstokenUrl (String code){
        String result = HttpUtil.get(GET_WEB_ACCESSTOKEN_URL.replace("APPID", APPID).replace("SECRET", APPSECRET).replace("CODE", code));
        JSONObject json = JSONObject.parseObject(result);

        return json;
    }
    /**
     * 获取用户信息getUserInfo
     *
     */
    public static JSONObject getUserInfo(String accessToken,String openId){
        String result = HttpUtil.get(GET_USERINFO_URL.replace("ACCESS_TOKEN", accessToken).replace("OPENID",openId));
        JSONObject json = JSONObject.parseObject(result);
        return json;
    }


    /**
     * 获取用户的UnionID
     *
     */
    public static JSONObject getUserUnionID(String accessToken,String openId){
        String result = HttpUtil.get(GET_USER_UNIONID.replace("ACCESS_TOKEN", accessToken).replace("OPENID",openId));
        JSONObject json = JSONObject.parseObject(result);
        return json;
    }
}
